<?php

/**
 * @copyright (C)2016-2099 Hnaoyun Inc.
 * @author XingMeng
 * @email hnxsh@foxmail.com
 * @date  2017年12月15日
 *  文章控制器
 */

namespace app\admin\controller\content;

use core\basic\Controller;
use app\admin\model\content\FuzhiModel;
use core\basic\Db;

class FuzhiController extends Controller {

    private $model;
    private $blank;

    public function __construct() {

        $this->model = new FuzhiModel();
    }

    // 一键复制
    /* $this->model->addContent($data)添加
     * $this->model->modContent($id, $data)更新
     * $this->model->delContentp($pid,$acode)删除
     * $list = post('list')数据id
     *   */
    function fuzhi() {
        $origin_area_tree = session('area_tree');
        $acodes = post('acodes');
        if (empty($acodes)) {
            $area_tree = session('area_tree');
//            error('没有选择要复制的分站!', -1);
        } else {
            $area_tree = [];
            $key_arae_tree = fieldAsKey($origin_area_tree, 'acode');
            foreach ($key_arae_tree as $area_tree_key => $area_tree_info) {
                if (in_array($area_tree_key, $acodes)) {
                    $area_tree[] = $area_tree_info;
                }
            }
        }
//        dump($area_tree);
//        die;

        $list = post('list');
//        dump($list);die;


        $arr = Db::table('ay_content')
                ->where("acode='cn'")
                ->in('id', $list)
                ->select(1);

        if (empty($arr)) {
            error('请选择数据!', -1);
        }

        foreach ($arr as $k => $v) {
            $v_id = $v['id'];
            foreach ($area_tree as $va) {
                if ($va->acode == 'cn') {
                    continue;
                }
                $scode = Db::table('ay_content_sort')->where("acode='{$va->acode}'")->where("pid='{$v['scode']}'")->value('scode');
                if (empty($scode)) {
                    continue;
                }
                $v_content_find = Db::table('ay_content')->where(['title' => $v['title'], 'scode' => $scode, 'acode' => $va->acode])->find(1);
                if ($v_content_find) {
                    continue;
                }

                // 查找扩展内容
                $extdata = Db::table('ay_content_ext')->where('contentid=' . $v_id)->find(1);


                // 去除主键并修改栏目
                $insert_v = $v;
                unset($insert_v['id']);
                $insert_v['scode'] = $scode;
                $insert_v['acode'] = $va->acode;

                // 插入主内容
                $id = Db::table('ay_content')->insertGetId($insert_v); // 插入扩展内容
                if ($id && $extdata) {
                    unset($extdata['extid']);
                    $extdata['contentid'] = $id;
                    $result = Db::table('ay_content_ext')->insert($extdata);
                } else {
                    $result = $id;
                }
                //添加之前先删除acode pid
//                $this->model->delContentp($va->acode, $v->id);
            }
        }
        success('一键复制成功！', -1);
    }

    /* 一键复制栏目 */

    public function fuzhilm() {
        $origin_area_tree = session('area_tree');
        $acodes = post('acodes');
        if (empty($acodes)) {
            $area_tree = session('area_tree');
//            error('没有选择要复制的分站!', -1);
        } else {
            $area_tree = [];
            $key_arae_tree = fieldAsKey($origin_area_tree, 'acode');
            foreach ($key_arae_tree as $area_tree_key => $area_tree_info) {
                if (in_array($area_tree_key, $acodes)) {
                    $area_tree[] = $area_tree_info;
                }
            }
        }
//        
//        $origin_area_tree = session('area_tree');
//        $acodes = post('acodes');
//        if (!$acodes) {
//            error('没有选择要复制的分站!', -1);
//        }
//        $area_tree = [];
//        $key_arae_tree = fieldAsKey($origin_area_tree, 'acode');
//        foreach ($key_arae_tree as $area_tree_key => $area_tree_info) {
//            if (in_array($area_tree_key, $acodes)) {
//                $area_tree[] = $area_tree_info;
//            }
//        }

        $list = post('list');

        $arr = Db::table('ay_content_sort ')
                ->where("acode='cn'")
                ->in('scode', $list)
                ->select(1);

        if (empty($arr)) {
            error('请选择数据!', -1);
        }

        foreach ($arr as $k => $v) {
            $v_id = $v['id'];
			$v_scode = $v['scode'];
            foreach ($area_tree as $va) {
                if ($va->acode == 'cn') {
                    continue;
                }
                $lastcode = Db::table('ay_content_sort')->order('id DESC')->value('scode');
                $scode = get_auto_code($lastcode);

                $v_content_find = Db::table('ay_content_sort')->where(['name' => $v['name'], 'mcode' => $v['mcode'], 'acode' => $va->acode])->find(1);
                if ($v_content_find) {
                    continue;
                }
                // 去除主键并修改栏目
                $insert_v = $v;
                unset($insert_v['id']);
                $insert_v['scode'] = $scode;
                $insert_v['acode'] = $va->acode;
				//                $insert_v['pid'] = $v_id;//sugao_edit，2020年6月18日20:54:26
                $insert_v['pid'] = $v_scode;
	
                // 插入主内容
                $this->model->addSort($insert_v); //添加
            }
        }

        //上下级关系处理
        foreach ($area_tree as $va) {
            if ($va->acode == 'cn') {
                continue;
            }
            $as = $this->model->findContentsortf($va->acode);
            //pid pcode acode
            foreach ($as as $v) {
                $a = $this->model->findContentsortfs($va->acode, $v->pcode);
                //parent::table('ay_content_sort')->where("acode='$acode'")->where("pid='$pcode'")->find();
                //修改pcode
                if ($a) {
                    $this->model->modSortSorup($v->id, array('pcode' => $a->scode));
                }
            }
        }

        success('一键复制成功！', -1);
    }

}
